package cn.edu.sdust.controller;

import cn.edu.sdust.pojo.Student;
import cn.edu.sdust.service.StudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class StudentController {

    @Autowired
    private StudentService service;

    @RequestMapping("/login")
    public String homePage() {
        return "login/login";
    }

    @RequestMapping("/main")
    public String mainPage() {
        return "main/main";
    }

    @RequestMapping("/select")
    public String selectPage() {
        return "select/select";
    }

    @RequestMapping("/insert")
    public String insertPage() {
        return "insert/insert";
    }

    @RequestMapping("/update")
    public String updatePage() {
        return "update/update";
    }

    /**
     * 跳转到列表页
     *
     * @return
     */
    @RequestMapping("/manage")
    public String list(Map<String, Object> map, Student student, Integer pageNo, Integer pageSize) {
        if (pageNo == null) {
            pageNo = 1;
        }

        if (pageSize == null) {
            pageSize = 5;
        }

        // 1.要查询出所有业务数据
        // 自动完成分页
        PageHelper.startPage(pageNo, pageSize);

        // 查询所有学生
        List<Student> students = service.QueryALLStudents(student);

        // 此实例记录分页相关数据
        PageInfo<Student> pi = new PageInfo<>(students);
        map.put("pages", pi.getPages());

        // 计算出导航页条的起始页和结束页
        int start = pageNo - 2;
        if (start < 1) {
            start = 1;
        }
        int end = start + 4;// 结束页
        if (end > pi.getPages()) {
            end = pi.getPages();
        }
        map.put("pageStart", start);
        map.put("pageEnd", end);
        map.put("pageNo", pageNo);
        map.put("total", pi.getTotal());
        map.put("pageSize", pageSize);
        // 前台传递过来的查询条件
        map.put("searchBean", student);
        // 将数据存储到域对象中
        map.put("students", students);
        // 在页面中以html格式显示数据
        return "manage/manage";
    }


    /**
     * 跳转到修改学生页面
     *
     * @return
     */
    @GetMapping("/edit")
    public String gotoEdit(Integer id, Map<String, Object> map) {
        if (id == null) {
            map.put("error", "要修改的学生编号不可为空");
            return "student/list";
        }
        Student stu = service.queryStudentById(id);
        if (stu == null) {
            map.put("error", "要修改的学生不存在");
            return "student/list";
        }

        // 把学生信息存储到域对象中
        map.put("stu", stu);

        return "student/edit";
    }

    /**
     * ajax删除指定编号的学生
     *
     * @param ids
     * @return
     */
    //返回过程中json自动的将map进行序列化
    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    //返回一个map的参数
    public ResponseEntity<Map<String, Object>> delete(Integer[] ids) {
        Map<String, Object> resp = new HashMap<>();
        if (ids == null || ids.length == 0) {
            //是否成功操作的标志
            resp.put("success", false);
            resp.put("error", "要删除的学生编号不可为空");
            return ResponseEntity.ok(resp);
        }

        int rows = this.service.deleteStudentById(ids);
        if (rows > 0) {
            resp.put("success", true);
            return ResponseEntity.ok(resp);
        } else {
            resp.put("success", false);
            resp.put("error", "删除失败");
            return ResponseEntity.ok(resp);
        }
    }

    @GetMapping("/add")
    public String gotoAdd() {
        return "student/add";
    }
    @PostMapping("/add")
    public String addPage(@RequestBody Student student, Map<String, Object> model) {
        System.out.println(student);
        System.out.println("\n\n\n\n\n");
        if (student.getStuId() == null || student.getStuId().trim().equals("")) {
            model.put("success", false);
            model.put("error", "学号不允许为空");
            return "student/add";
        }

        if (student.getName() == null || student.getName().trim().equals("")) {
            model.put("success", false);
            model.put("error", "姓名不允许为空");
            return "student/add";
        }

        // 保存
        System.out.println("\n\n3\n\n");
        boolean success = service.addStudent(student);
        System.out.println(success);
        model.put("success", success);
        return "student/add";
    }

    /**
     * 跳转到修改学生页面
     *
     * @return
     */
    @GetMapping("/update")
    public String gotoUpdate(Integer id, Map<String, Object> map) {

        System.out.println("111");
        System.out.println(id);
        if (id == null) {
            map.put("error", "要修改的学生编号不可为空");
            return "main/main";
        }

        System.out.println("111");
        System.out.println(id);

        Student stu = service.queryStudentById(id);

        if (stu == null) {
            map.put("error", "要修改的学生不存在");
            return "main/main";
        }

        // 把学生信息存储到域对象中
        map.put("stu", stu);

        return "update/update";
    }

    @PostMapping("/update")
    public String updatePage(@RequestBody Student student, Map<String, Object> model) {
        System.out.println(student);
        System.out.println("\n\n\n\n\n");
        if (student.getStuId() == null || student.getStuId().trim().equals("")) {
            model.put("success", false);
            model.put("error", "学号不允许为空");
            return "update/update";
        }

        if (student.getName() == null || student.getName().trim().equals("")) {
            model.put("success", false);
            model.put("error", "姓名不允许为空");
            return "update/update";
        }

        // 保存
        System.out.println("\n\n3\n\n");
        boolean success = service.update(student);
        System.out.println(success);
        model.put("success", success);
        return "manage/manage";
    }


}